<!DOCTYPE html>
<html>
<head>
<head />
<div th:include="base-libs :: base-libs" />
<div th:include="common/yljg-modal :: yljg-modal" />
<div th:include="common/fwxx-modal :: fwxx-modal" />
<title>服务包操作</title>
<style type="text/css">
.topBtn{
	float: right;
	margin-right:10px;
	margin-top:5px
}
.td-text{
	width:130px;
}
</style>
</head>
<script th:inline="javascript">
	/**服务项数组*/
	var fwxAry = [[${fwxAry}]];
	/**用户信息*/
	var userInfo = [[${userInfo}]];
</script>
<script type="text/javascript">
	$(function() {
		$(this).registerManagerPage({
	        cacheTables: ["TB_ZY_QY_TYPE","TB_ZY_SERVICE_TYPE"]
	        ,callback: initData
	    });
		initView();
	});

	function initData() {
		/**加载值域*/
		//$(".cacheTable").CacheForSelect();
		$("#managerForm").attr("action", "save");
		/**赋值当前登录用户机构信息*/
		$("#YLJGMC").val(userInfo["YLJGMC"]);
	   	$("#YLJGID").val(userInfo["PTJGID"]);
	   	/**当前机构非平台时禁用选择机构*/
	   	if((userInfo["WSJGLBDM"]).substr(0,1) != "R"){
	   		$("#xzYljg").attr("disabled", "disabled");
	   	}
		/**加载服务包信息*/
		var fwbCache = StoreCache.removeCache("FWB_INFO");
		if(fwbCache){
			$("#method").val("update");
			$("#managerForm").find(".dataField").each(function(){
			    var key = $(this).attr("id");
                var value = fwbCache[key];
                if(key == "CJSJ" && !StringUtils.isEmpty(value)){
					value = value.substring(0,10);
				}
                $(this).val(value);
			});
			$("input[name='FWBZT']").each(function(){
				if($(this).val() == fwbCache["FWBZT"]){
					$(this).prop("checked",true);
				}else{
					$(this).prop("checked",false);
				}
			});
			if(fwxAry){
      			$("#dgForm").datagrid("loadData", {"total": fwxAry.length, "rows": fwxAry})
			}
		}else{
			$("#CJSJ").val(getCurrentTime());
			$("#FWBID").val(createCurrentBm());
			$("input[name='FWBZT']").each(function(){
				if($(this).val() == "1"){
					$(this).prop("checked",true);
				}else{
					$(this).prop("checked",false);
				}
			});
		}
	}

	function initView() {
	    if(!NFTJ_YBFWX){//能否添加医保服务项
			$("#SFBHYBFWX").parents("tr").remove();
		}
		$("#dgForm").datagrid({
			idField : "FWID",
			uniqueId: "FWID",
			striped : true,
			border:false,
			clickToSelect: true,
			columns : [[
				{
					field : "FWID",
					width: 140,
					align: "center",
					title : "服务项编号"
				},
				{
					field : "FWMC",
					width: 100,
					align: "center",
					title : "服务项名称"
				},
				{
					field : "FWNR",
					width: 100,
					align: "center",
					title : "服务内容",
					formatter : function(value, row, index){
						return "<span title='"+value+"'>"+value+"</span>";
					}
				},
				{
					field : "YLJGMC",
					width: 100,
					align: "center",
					title : "所属机构"
				},
				{
					field : "QYLB",
					width: 100,
					align: "center",
					title : "签约类别",
					formatter:function(value,row){
						var qyType = StoreCache.getCache('TB_ZY_QY_TYPE');
						$.each(qyType.data, function(i,item){
							if(item.XMDM == value){
								value = item.XMMC;
								return false;
							}
						})
						return value;
					}
				},
				{
					field : "FWLBMC",
					width: 70,
					align: "center",
					title : "服务类别"
				},
				{
					field : "SFBZ",
					width: 150,
					align: "center",
					title : "收费标准",
					formatter : function(value, row, index){
						return getSflx(row["SFLX"]) + "," + value + getFydw(row["FYDW"]);
					}
				},
				
				{
					field : "FWPC",
					align: "center",
					width: 70,
					title : "服务频次"/* ,
					formatter : function(value, row, index){
						if(row["FWPCLX"]=="01"){
							value = "按次计算";
						}else if(row["FWPCLX"]=="02"){
							value = "按签约周期计算";
						}
						return value + ":" + row["FWPC"];
					} */
				},
				{
					field : "FWZT",
					align: "center",
					width: 70,
					title : "状态",
					formatter : function(value, row, index){
						return getFwzt(value);
					}
				},
				{
					field : "CJSJ",
					width: 90,
					align: "center",
					title : "创建日期",
					formatter : function(value, row, index){
						if(!StringUtils.isEmpty(value)){
							value = value.substr(0, 10);
						}
						return value;
					}
				},
				{
					field : "cz",
					width: 150,
					align: "center",
					title : "操作",
					formatter : operateFormatter
				}
			]]
			,pageNumber: 1
			,pageSize: 100
			,fit: true
			,fitColumns: true
			,singleSelect: false
			,rownumbers: true
		});
		
		
		
		
		/**加载医疗机构信息*/
		loadYljg();
		/**加载服务项信息*/
		loadFwxx();
		/**保存按钮*/
		$("#btnSave").on("click", function(){
			if($(".form-control").QuickValidate() && validateFwx()){
				/**获取服务状态*/
				/* var fwbzt;
				$("input[name='FWBZT']").each(function(){
					if($(this).prop("checked")){
						fwbzt = $(this).val();
					}
				}); */
				/**获取服项ID数组*/
				var fwxids = [];
				var ary = $("#dgForm").datagrid("getRows");
				$(ary).each(function(index, item){
					fwxids.push({FWID: item["FWID"], FWPCLX: item["FWPCLX"], FWPC: item["FWPC"]});
				});
				$.QuickRemote.AjaxFiledFormSubmit("managerForm", function(result) { 
					var info = result["info"];
					if (result["state"]) {
						$.QuickAlert.alertNormal("操作成功！",{callback: function(){
							$.QuickUrlUtils.redirectUrl("index.html");
						}});
					} else {
						$.QuickAlert.alertFail({
							content : info["faultInfo"]
						});
					}
				},{ FWBZT : "1", FWXS: JSON.stringify(fwxids)}); 
			}
		})

		//服务包类型与优惠后价格--联动
		$("#FWBLX").change(function(){
		    var val = $(this).val();
		    if(val == '01'){
                //免费服务包
				$("#FY").val(0).prop("readonly","readonly").css("cursor","not-allowed");
			}else{
                $("#FY").removeAttr("readonly").css("cursor","default");
			}
			if(val == '03'){
		        $("#SFBHYBFWX").removeClass("form-control").val("");
		        $(this).parents("tr").next().css("visibility","hidden");
			}else{
                $("#SFBHYBFWX").addClass("form-control");
                $(this).parents("tr").next().css("visibility","visible");
			}
		})
	}

	function operateFormatter(value, row, index){
		return ($("#ycBtn").html()).replace(/#index#/g,index);
		
	}
	
	/**加载医疗机构信息*/
	function loadYljg(){
		$("#xzYljg").on("click", function(){
			selectionId = $("#YLJGID").val();
			//页面层
			parent.layer.open({
			  type: 1,
			  area: ["80%", "500px"], //宽高
			  scrollbar: false,
			  resize: false,
			  title: '选择所属机构',
			  content: $("#yljgModal").html(),
			  btn: ["确定", "取消"],
			  success: function(layero, index){
				  
				  initYljgTable(layero);
			  },
			  yes: function (index, layero) {
              			var info = $(layero).find("#yljgForm").datagrid("getSelected")
             			$("#YLJGMC").val(info["YLJGMC"]);
   			  			$("#YLJGID").val(info["PTJGID"]);
   			  			parent.layer.close(index);//这块是点击确定关闭这个弹出层
              }
			});
			
		});
	}
	
	/**加载服务项信息*/
	function loadFwxx(){
		
		$("#xzFwxx").on("click", function(){
			selection_base = $("#dgForm").datagrid("getRows");
            selection = [].concat(selection_base);

			//页面层
			parent.layer.open({
			  type: 1,
			  area: ["80%", "530px"], //宽高
			  scrollbar: false,
			  resize: false,
			  content: $("#fwxx").html(),
			  btn: ["确定", "下一步", "取消"],
			  btn2: function(layero, index){return false;},
			  success: function(layero, index){
				    initFwxx(layero,$("#YLJGID").val());
			  },
			  yes: function (index, layero) {
				  	if($(layero).find(".form-control").QuickValidate()){
	           			//var rows = $(layero).find("#fwxxForm").datagrid("getSelections");
	           			var rows = $(layero).find("#addFwpc").datagrid("getRows");
	           			$("#dgForm").datagrid("loadData", {"total": rows.length, "rows": rows})
	           			parent.layer.close(index);//这块是点击确定关闭这个弹出层
				  	}
              }
			});
			
		});
	}
	
	/**返回按钮*/
	function btnBackHandler() {
		$.QuickUrlUtils.redirectUrl("index.html");
	}
	
	/**获取当前时间 <公用>*/
	function getCurrentTime(){
		var date = new Date();
	    var seperator1 = "-";
	    var month = date.getMonth() + 1;
	    var strDate = date.getDate();
	    if (month >= 1 && month <= 9) {
	        month = "0" + month;
	    }
	    if (strDate >= 0 && strDate <= 9) {
	        strDate = "0" + strDate;
	    }
	    var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate;
	    return currentdate;
	}
	
	/**获取当前编码*/
	function createCurrentBm(){
		var currentBm = "P";
		var date = new Date();
	    var month = date.getMonth() + 1;
	    var strDate = date.getDate();
	    if (month >= 1 && month <= 9) {
	        month = "0" + month;
	    }
	    if (strDate >= 0 && strDate <= 9) {
	        strDate = "0" + strDate;
	    }
	    currentBm += date.getFullYear() + "" + month + strDate + date.getHours()
	    + date.getMinutes() + date.getSeconds() + date.getMilliseconds();
	    return currentBm;
	}
	
	
	function validateZt(){
		var flag = false;
		$("input[name='FWBZT']").each(function(){
			if($(this).prop("checked")){
				flag = true;
			}
		});
		if(!flag){
			$.QuickAlert.alertFail({
				content : "请选择服务包状态！",
			});
		}
		return flag 
	}
	
	/**移除服务项*/
	function remove(index){
		$("#dgForm").datagrid("deleteRow",index);
		var data = $("#dgForm").datagrid("getData");
		/**清除所有数据*/
		$("#dgForm").datagrid("loadData", { total: 0, rows: [] }); 
		/**重新加载数据*/
		$("#dgForm").datagrid("loadData",data);
	}
	
	/**校验服务项*/
	function validateFwx(){
		var ary = $("#dgForm").datagrid("getRows");
		var flag = false;
		if(ary.length > 0){
				flag = true;
		}else{
			$.QuickAlert.alertFail({
				content : "请选择服务项！",
			});
		}
		return flag 
	}
</script>

<body class="easyui-layout">
	<div region="north" style="border: none; overflow: hidden;height:290px">
		<h4 class="h4-title" style="font-size: 18px;">
			服务包信息
			<button class="btn topBtn" style="float:right" type="button" onclick="btnBackHandler()">返回</button>
			<button class="btn topBtn" type="button" id="btnSave" >提交</button>
			<!-- <div style="float:right;margin-top:-7px;">
				<button class="btn btn-primary" type="button" id="btnSave" >提交</button>
				&nbsp;
				<button class="btn btn-default" type="button" onclick="btnBackHandler()">返回</button>
			</div> -->
		</h4>
		<div class="form-table-box" >
			<form id="managerForm" class="form-horizontal" >
				<div style="display: none;" >
					<input id="YLJGID" name="YLJGID" class="dataField"/>
					<input id="method" name="method" value="add"/>
				</div>
				<table class="form-table">
						<tr>
							<td class="td-text">
								<label>服务包编码：</label>
							</td>
							<td class="td-content">
								<input id="FWBID" name="FWBID" class="form-control dataField" type="text"
									readonly="readonly"	placeholder="服务包编码">
							</td>
							<td class="td-text">
								<label>服务包名称：</label>
							</td>
							<td class="td-content">
								<input id="FWBMC" name="FWBMC" class="form-control dataField" type="text"
									quick-config="label:'服务包名称', validateProxy:'notNull length', length: '100'"
									placeholder="服务包名称">
							</td>
							<td class="td-text">
								<label>创建时间：</label>
							</td>
							<td class="td-content">
								<input id="CJSJ" name="CJSJ" readonly="readonly" class="form-control dataField" type="text"/>
							</td>
						</tr>
						<tr>
							<td class="td-text">
								<label>所属机构：</label>
							</td>
							<td class="td-content">
								<input id="YLJGMC" class="form-control dataField" readonly="readonly" style="width: 58%;"/>
								<!-- 按钮触发模态框 -->
								<button class="btn" type="button"  style="width: 30%;padding:0"  id="xzYljg">选择</button>
							</td>
							<!-- <td class="td-text">有效期：</td>
							<td class="td-content">
								<select id="YXQ" name="YXQ" class="form-control dataField cacheTable"
								quick-config="label:'有效期', validateProxy:'notNull', tableName:'TB_ZY_YXQ'">
									<option value="">请选择</option>
									<option value="01">一个月</option>
									<option value="02">三个月</option>
									<option value="03">半年</option>
									<option value="04">一年</option>
								</select>
							</td> -->
							<td class="td-text">
								<label>成本测算价(元)：</label>
							</td>
							<td class="td-content">
								<input id="CBCSJ" name="CBCSJ" class="form-control dataField" type="text"
									quick-config="label:'成本测算价', validateProxy:'length num', length: '10'"
									placeholder="成本测算价" value="0">
							</td>
							<td class="td-text">
								<label>优惠后价格(元)：</label>
							</td>
							<td class="td-content">
								<input id="FY" name="FY" class="form-control dataField" type="text"
									   quick-config="label:'优惠后价格', validateProxy:'notNull length num', length: '10'"
									   placeholder="优惠后价格">
							</td>
						</tr>
						<tr>
							<td class="td-text">
								<label>签约对象：</label>
							</td>
							<td class="td-content">
								<select id="QYDX" name="QYDX" cacheTable="TB_ZY_QYDX"  class="form-control dataField" style="width: 90%" ></select>
							</td>
							<td class="td-text">
								<label>公卫经费支付费用：</label>
							</td>
							<td class="td-content">
								<input id="GWJFZF" name="GWJFZF" class="form-control dataField" type="text"
									   quick-config="label:'公卫经费支付费用', validateProxy:'length num', length: '10'"
									   placeholder="基本公卫经费支付费用" value="0">
							</td>
							<td class="td-text">
								<label>医保基金支付费用：</label>
							</td>
							<td class="td-content">
								<input id="YBJJZF" name="YBJJZF" class="form-control dataField" type="text"
									   quick-config="label:'医保基金支付费用', validateProxy:'length num notNull', length: '10'"
									   placeholder="医保基金支付费用" >
							</td>
						</tr>
						<tr>
							<td class="td-text">
								<label>医保个人自付费用：</label>
							</td>
							<td class="td-content">
								<input id="YBGRZF" name="YBGRZF" class="form-control dataField" type="text"
									   quick-config="label:'医保个人自付费用', validateProxy:'length num', length: '10'"
									   placeholder="医保个人自付费用" value="0">
							</td>
							<td class="td-text">
								<label>居民自费费用：</label>
							</td>
							<td class="td-content">
								<input id="JMZFFY" name="JMZFFY" class="form-control dataField" type="text" style="width: 90%"
									   quick-config="label:'居民自费费用', validateProxy:'length num', length: '10'"
									   placeholder="居民自费费用" value="0">
							</td>
							<td class="td-text">
								<label>服务包类型：</label>
							</td>
							<td class="td-content">
								<select id="FWBLX" name="FWBLX" cacheTable="TB_ZY_FWBLX"  class="form-control dataField" style="width: 90%"
									quick-config="label:'服务包类型', validateProxy:'notNull'"></select>
							</td>
						</tr>
						<tr>
							<td class="td-text">
								<label>是否包含医保服务项：</label>
							</td>
							<td class="td-content" colspan="5">
								<select id="SFBHYBFWX" name="SFBHYBFWX"  class="form-control dataField" style="width: 97.8%"
										quick-config="label:'是否包含医保服务项', validateProxy:'notNull'">
									<option value="">请选择</option>
									<option value="1">是</option>
									<option value="0">否</option>
								</select>
							</td>
						</tr>
					</table>
			</form>
			<h4 class="h4-title" style="font-size: 18px;">
				服务项信息
				<div style="float:right;margin-top:-7px;">
					<button class="btn" type="button" id="xzFwxx">选择服务项</button>
				</div>
			</h4>
		</div>
	</div>
	<div region="center" style="border: none;">
			<table id="dgForm">
			<!-- <tr>
				<td>服务项编号</td>
				<td>服务项名称</td>
				<td>服务内容</td>
				<td>所属机构</td>
				<td>服务类别</td>
				<td>收费标准</td>
				<td>操作</td>
			</tr> -->
			</table>
	</div>

<script type="text/html" id="ycBtn">
<a class="tableToolBarBtn" href="javascript:void(0)" title="移除" onclick="remove(#index#)">移除</a>
</script>

</body>
</html>


